package club.kingyin.kyblog.controller;

import club.kingyin.kyblog.core.business.dao.web.Focus;
import club.kingyin.kyblog.core.business.dao.web.Friend;
import club.kingyin.kyblog.core.business.dao.web.Social;
import club.kingyin.kyblog.core.business.dto.ResultUtil;
import club.kingyin.kyblog.core.business.service.admin.TypeService;
import club.kingyin.kyblog.core.business.service.web.SiteService;
import club.kingyin.kyblog.core.business.vo.web.SiteVo;
import club.kingyin.kyblog.core.utils.VoUtil;
import club.kingyin.kyblog.core.utils.cache.Cache;
import club.kingyin.kyblog.core.utils.log.LogConsole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Set;

/**
 * @author : kingyin
 * @version : 1.0
 * @program : Ky-Blog
 * @description : 在此处添加类描述
 * @create : 2021年08月12日 14:41
 **/

@RestController
@RequestMapping("/blog")
public class SiteController {

    @Autowired
    private SiteService siteService;

    @Autowired
    private TypeService typeService;

//    @Cache(module = "站点", operator = "九命可", getExpire = true, expire = 1000 * 60 * 60 * 24)
    @LogConsole(module = "站点", operator = "九命可")
    @RequestMapping("/site")
    public ResultUtil site() {
        return ResultUtil.success(siteService.getSiteInfo());
    }

    @LogConsole(module = "资源", operator = "九命可")
    @RequestMapping("/resources")
    public ResultUtil resources() {
        return ResultUtil.success(siteService.getResources());
    }

    @LogConsole(module = "关于", operator = "九命可")
    @RequestMapping("/about")
    public ResultUtil about() {
        return ResultUtil.success(siteService.getAbout());
    }

    @LogConsole(module = "聚焦", operator = "九命可")
    @RequestMapping("/focus/list")
    public ResultUtil focus() {
        Focus[] focus = siteService.getFocus();
        if (focus == null) {
            return ResultUtil.fail("聚焦信息异常");
        } else {
            return ResultUtil.success(focus);
        }
    }

    @LogConsole(module = "社交", operator = "九命可")
    @RequestMapping("/social")
    public ResultUtil social() {
        Social[] socials = siteService.getSocial();
        if (socials == null) {
            return ResultUtil.fail("社交信息异常");
        } else {
            return ResultUtil.success(socials);
        }
    }

    @LogConsole(module = "友情链接", operator = "九命可")
    @RequestMapping("/friend")
    public ResultUtil friend() {
        ArrayList<Friend> friends = siteService.getFriends();
        if (friends == null) {
            return ResultUtil.fail("友链信息异常");
        } else {
            return ResultUtil.success(friends);
        }
    }

    @LogConsole(module = "提交友情连接", operator = "九命可")
    @PostMapping("/about/submit")
    public ResultUtil submit(@RequestBody Friend friend) {
        return siteService.addFriend(friend)? ResultUtil.success("提交成功，正在审核...") : ResultUtil.fail("当前提交人数过多，请稍后尝试");
    }

    // 需要缓存更新
    @Cache(module = "获取标签", operator = "九命可")
    @GetMapping("/category")
    public ResultUtil category() {
        Set<String> types = typeService.getTypes();
        if (types != null) {
            return ResultUtil.success(types);
        }
        return ResultUtil.fail("加载失败");
    }
}
